package club.xyes.zkh.ecommerce.web.controller;

import club.xyes.zkh.ecommerce.commons.context.ApplicationConstants;
import club.xyes.zkh.ecommerce.commons.dto.MinimalShopInfo;
import club.xyes.zkh.ecommerce.commons.dto.MinimalUserInfo;
import club.xyes.zkh.ecommerce.commons.http.holder.RequestExtendParamHolder;

import javax.validation.constraints.NotNull;

/**
 * Create by 郭文梁 2019/4/19 0019 13:30
 * ControllerSupport
 * 所有控制器的父类
 *
 * @author 郭文梁
 * @data 2019/4/19 0019
 */
public abstract class AbstractControllerSupport {
    /**
     * 获取基础用户信息
     *
     * @return 用户信息
     */
    protected MinimalUserInfo getUserInfo() {
        return RequestExtendParamHolder.get(ApplicationConstants.Security.ACCESS_TOKEN_USER_INFO_KEY, MinimalUserInfo.class);
    }

    /**
     * 获取用户信息 当用户信息为空时抛出异常
     *
     * @return 用户信息
     */
    @NotNull
    protected MinimalUserInfo getRequiredUserInfo() {
        MinimalUserInfo userInfo = getUserInfo();
        if (userInfo == null) {
            throw new IllegalStateException("Cached userInfo is None,");
        }
        return userInfo;
    }

    /**
     * 获取当前登录的商铺信息
     *
     * @return 商铺信息
     */
    public MinimalShopInfo getShopInfo() {
        return RequestExtendParamHolder.get(ApplicationConstants.Security.ACCESS_TOKEN_SHOP_INFO_KEY, MinimalShopInfo.class);
    }

    /**
     * 获取当前登录的shop信息 若不存在则抛出异常
     *
     * @return shopInfo
     */
    public MinimalShopInfo getRequiredShopInfo() {
        MinimalShopInfo shopInfo = getShopInfo();
        if (shopInfo == null) {
            throw new IllegalStateException("Cached shopInfo is None,");
        }
        return shopInfo;
    }
}
